﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.Base;
    using UFIDA.U9.MO.Enums;
    using UFIDA.U9.MO.MO;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;

	/// <summary>
	/// MOUnapproveSV partial 
	/// </summary>	
	public partial class MOUnapproveSV 
	{	
		internal BaseStrategy Select()
		{
			return new MOUnapproveSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class MOUnapproveSVImpementStrategy : BaseStrategy
	{
		public MOUnapproveSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			MOUnapproveSV bpObj = (MOUnapproveSV)obj;

			try
			{
				if (bpObj.Data == null || bpObj.Data.Count == 0)
					throw new Exception("传入参数为空");

				List<CopyOfCommonArchiveDataDTOData> retlist = new List<CopyOfCommonArchiveDataDTOData>();
				foreach (DocDTOData doc in bpObj.Data)
				{
					MO be = MO.Finder.Find($"DocNo='{doc.DocNo}'", null);
					if (be == null)
						throw new Exception($"单号：[{doc.DocNo}]查找不到数据");
					if (be.DocState != MOStateEnum.Approved)
						throw new Exception($"单号：[{doc.DocNo}]状态不为审核，不能弃审！");

					using (ISession session = Session.Open())
					{
						be.DocState = MOStateEnum.Opened;
						be.WFOriginalState = 2;
						be.WFCurrentState = 0;
						be.IsCounteractPP = false;
						be.CancelApproveBy = Context.LoginUser;
						be.CancelApproveOn=DateTime.Now;
						be.BusinessCreatedOn=DateTime.Now;
						be.ApproveBy = "";
						be.ApproveOn = DateTime.MinValue;
						be.ActualStartDate = DateTime.MinValue;

						session.Commit();
					}

					//处理返回
					CopyOfCommonArchiveDataDTOData ret = new CopyOfCommonArchiveDataDTOData();
					ret.ID = be.ID;
					ret.Code = be.DocNo;
					retlist.Add(ret);
				}
				return ResultUtils.success(bpObj, bpObj.Data, retlist);
			}
			catch (Exception ex)
			{
				return ResultUtils.error(bpObj, bpObj.Data, ex);
			}
		}		
	}

	#endregion
	
	
}